﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StudentsInBinaryFile
{
    
    class Student
    {
        private string firstName;
        private string lastName;
        private int fn;

        public Student(string firstName, string lastName, int fn)
        {
            this.firstName = firstName;
            this.lastName = lastName;
            this.fn = fn;
        }

        public string FirstName
        {
            get { return firstName; }
        }
        
        public string LastName
        {
            get { return lastName; }
        }
        
        public int Fn
        {
            get { return fn; }
        }

        public override string ToString()
        {
            string result = Fn + ": " + FirstName + " " + LastName;
            return result;
        }
    }

    class StudentsInBinaryFile
    {
        static char Separator = Path.DirectorySeparatorChar;
        static string InputFileName = ".." + Separator + ".." + Separator + "file.bin";

        public static void WriteStudentToFile(Student student)
        {
            StreamWriter writer = new StreamWriter(InputFileName,true);
            using (writer)
            {
                writer.WriteLine(student.ToString());
            }
        }

        public static void PrintAll()
        {
            StreamReader reader = new StreamReader(InputFileName);
            using (reader)
            {
                Console.WriteLine("All students:");
                string student = reader.ReadLine();
                while (student != null)
                {
                    Console.WriteLine(student);
                    student = reader.ReadLine();
                }
            }
        }

        public static void DeleteStudentByFn(int fn)
        {
            string[] students;
            StreamReader reader = new StreamReader(InputFileName);
            using(reader)
            {
                string[] splitters = {Environment.NewLine};
                students = reader.ReadToEnd().Split(splitters, StringSplitOptions.RemoveEmptyEntries);
            }
            StreamWriter writer = new StreamWriter(InputFileName);
            using(writer)
            {
                foreach (string student in students)
	            {
		            if(!student.Contains(fn.ToString()))
                    {
                        writer.WriteLine(student);
                    }
	            }
            }
        }

        static void Main(string[] args)
        {
            Student todor = new Student("Todor", "Petrov", 12345);
            Student petar = new Student("Petar", "Petrov", 12122);
            Student gosho = new Student("Gosho", "Goshev", 22334);
            WriteStudentToFile(todor);
            WriteStudentToFile(petar);
            PrintAll();
            WriteStudentToFile(gosho);
            PrintAll();
            DeleteStudentByFn(12122);
            PrintAll();
        }
    }
}
